import {ref, toRefs} from "vue";
import useDictStore from "@/store/modules/dict";
import {getDictByType} from "@/api/system/dict_data";

export default {
    useDict(...args) {
        const res = ref({})
        return (() => {
            args.forEach((dictType, index) => {
                res.value[dictType] = [];
                const dicts = useDictStore().getDict(dictType);
                if (dicts) {
                    res.value[dictType] = dicts;
                } else {
                    getDictByType(dictType).then(resp => {
                        res.value[dictType] = resp.data.map(item => ({
                            label: item.dictDataName,
                            value: item.dictDataValue,
                            elTagType: item.listClass,
                            elTagClass: item.cssClass
                        }))
                        useDictStore().setDict(dictType, res.value[dictType])
                    })
                }
            })
            return toRefs(res.value)
        })()
    }
}
